package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateListActivity2 extends AppCompatActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener{
    Handler handler;
    ListView listView;
    MyAdapter myAdapter;
    private static final String TAG = "RateListActivity2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list2);


        ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ItemTitle", "Rate: " + i);
            map.put("ItemDetail", "detail" + i);
            listItems.add(map);

        }
        SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItems, R.layout.list_item,
                new String[]{"ItemTitle", "ItemDetail"},
                new int[]{R.id.title, R.id.detail});

        listView = findViewById(R.id.mylist2);
        listView.setAdapter(listItemAdapter);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);

        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 5) {
                    List<HashMap<String, String>> list = (List<HashMap<String, String>>) msg.obj;
                    SimpleAdapter listItemAdapter = new SimpleAdapter(RateListActivity2.this, listItems, R.layout.list_item,
                            new String[]{"cname", "cval"},
                            new int[]{R.id.title, R.id.detail});

                    myAdapter = new MyAdapter(RateListActivity2.this, R.layout.list_item, (ArrayList<HashMap<String, String>>) list);

                    listView.setAdapter(myAdapter);
                }
                super.handleMessage(msg);
            }

        };

        MapTask mapTask = new MapTask();
        mapTask.setHandler(handler);

        Thread t = new Thread(mapTask);
        t.start();

    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap<String, String> map = (HashMap<String, String>) listView.getItemAtPosition(position);
        Log.i(TAG, "onItemClick:position=" + position);
        Log.i(TAG, "onItemClick:cname=" + map.get("cname"));
        Log.i(TAG, "onItemClick:cval=" + map.get("cval"));



        }

    public boolean onItemLongClick(AdapterView<?> parent,View view,int position,long id){
        Log.i(TAG,"onItemLongClick:pos" +position);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示")
                    .setMessage("请确认书否删除当前数据")
                    .setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int which) {
                            Log.i(TAG, "onClick:对话框事件处理");
                            myAdapter.remove(listView.getItemAtPosition(position));

                        }
                    }).setNegativeButton("否",null);
            builder.create().show();
        return  true;
    }


}